package com.itfreer.power.dao.user;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;

import com.itfreer.form.api.JdbcBaseEntity;
import com.itfreer.power.entity.user.UserInfoEntity;
import com.itfreer.power.entity.user.UserRoleEntity;

/**
 * 定义用户详细信息实体
 */
@Entity(name = "sso_user_info")
public class JdbcUserInfoEntity implements JdbcBaseEntity<UserInfoEntity>, Serializable {

	private static final long serialVersionUID = 3065523710938752296L;

	/**
	 * 帐号 唯一值
	 */
	@Id
	@Column(name = "id", length = 50, nullable = false)
	private String userid;

	/**
	 * 所属机构
	 */
	@Column(name = "organization_id", length = 50, nullable = true)
	private String organizationId;

	/**
	 * 所属部门
	 */
	@Column(name = "department_id", length = 50, nullable = true)
	private String departmentId;

	/**
	 * 所属岗位
	 */
	@Column(name = "operating_post_id", length = 50, nullable = true)
	private String operatingPostId;

	/**
	 * 所属职位
	 */
	@Column(name = "position_id", length = 50, nullable = true)
	private String positionId;

	/**
	 * 所属工作组
	 */
	@Column(name = "working_group_id", length = 50, nullable = true)
	private String workingGroupId;

	/**
	 * 用户名
	 */
	@Column(name = "user_name", length = 50, nullable = true)
	private String userName;

	/**
	 * 性别
	 */
	@Column(name = "sex", length = 50, nullable = true)
	private String sex;

	/**
	 * 生日
	 */
	@Column(name = "birth", nullable = true)
	private Date birth = null;

	/**
	 * 联系电话
	 */
	@Column(name = "tel", length = 50, nullable = true)
	private String tel;

	/**
	 * 邮件
	 */
	@Column(name = "email", length = 50, nullable = true)
	private String email;

	/**
	 * 照片
	 */
	@Column(name = "photo", length = 50, nullable = true)
	private String photo;

	/**
	 * 住址所在省
	 */
	@Column(name = "sheng", length = 50, nullable = true)
	private String sheng;

	/**
	 * 住址所在市
	 */
	@Column(name = "shi", length = 50, nullable = true)
	private String shi;

	/**
	 * 住址所在县
	 */
	@Column(name = "xian", length = 50, nullable = true)
	private String xian;

	/**
	 * 住址具体地址
	 */
	@Column(name = "local", length = 200, nullable = true)
	private String local;

	/**
	 * 学历
	 */
	@Column(name = "degree", length = 50, nullable = true)
	private String degree;

	/**
	 * 民族
	 */
	@Column(name = "nation", length = 50, nullable = true)
	private String nation;

	/**
	 * 入职时间
	 */
	@Column(name = "enter_time", nullable = true)
	private Date enterTime = null;

	/**
	 * 毕业学校
	 */
	@Column(name = "graduate_school", length = 50, nullable = true)
	private String graduateSchool;

	/**
	 * 其它属性json字符串
	 */
	@Column(name = "other_attributes_json", length = 5000, nullable = true)
	private String otherAttributesJson;

	/**
	 * 子表对象
	 */
	@OneToOne(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER)
	@JoinColumn(name = "id")
	private JdbcUserAuthenticationEntity authentication;

	/**
	 * 角色信息
	 */
	@SuppressWarnings("deprecation")
	@org.hibernate.annotations.ForeignKey(name = "none")
	@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY)
	@JoinColumn(name = "user_id")
	private Set<JdbcUserRoleEntity> roles = new HashSet<JdbcUserRoleEntity>();
	
	/**
	 *社会信用码
	 */
	@Column(name = "social_credit_code", length = 50, nullable = true)
	private String  socialCreditCode;
	/**
	 * 是否有效
	 */
	@Column(name = "job_statu", nullable = true)
	private Boolean jobStatu = null;

	/**
	 * 身份证验证是否通过
	 */
	@Column(name = "isIdCheck", nullable = true)
	private Boolean isIdCheck = false;
	
	/**
	 * 帐号 唯一值
	 */
	public String getUserid() {
		return userid;
	}

	/**
	 * 帐号 唯一值
	 */
	public void setUserid(String userid) {
		this.userid = userid;
	}

	/**
	 * 所属机构
	 */
	public String getOrganizationId() {
		return organizationId;
	}

	/**
	 * 所属机构
	 */
	public void setOrganizationId(String organizationId) {
		this.organizationId = organizationId;
	}

	/**
	 * 所属部门
	 */
	public String getDepartmentId() {
		return departmentId;
	}

	/**
	 * 所属部门
	 */
	public void setDepartmentId(String departmentId) {
		this.departmentId = departmentId;
	}

	/**
	 * 所属岗位
	 */
	public String getOperatingPostId() {
		return operatingPostId;
	}

	/**
	 * 所属岗位
	 */
	public void setOperatingPostId(String operatingPostId) {
		this.operatingPostId = operatingPostId;
	}

	/**
	 * 所属职位
	 */
	public String getPositionId() {
		return positionId;
	}

	/**
	 * 所属职位
	 */
	public void setPositionId(String positionId) {
		this.positionId = positionId;
	}

	/**
	 * 所属工作组
	 */
	public String getWorkingGroupId() {
		return workingGroupId;
	}

	/**
	 * 所属工作组
	 */
	public void setWorkingGroupId(String workingGroupId) {
		this.workingGroupId = workingGroupId;
	}

	/**
	 * 用户名
	 */
	public String getUserName() {
		return userName;
	}

	/**
	 * 用户名
	 */
	public void setUserName(String userName) {
		this.userName = userName;
	}

	/**
	 * 性别
	 */
	public String getSex() {
		return sex;
	}

	/**
	 * 性别
	 */
	public void setSex(String sex) {
		this.sex = sex;
	}

	/**
	 * 生日
	 */
	public Date getBirth() {
		return birth;
	}

	/**
	 * 生日
	 */
	public void setBirth(Date birth) {
		this.birth = birth;
	}

	/**
	 * 联系电话
	 */
	public String getTel() {
		return tel;
	}

	/**
	 * 联系电话
	 */
	public void setTel(String tel) {
		this.tel = tel;
	}

	/**
	 * 邮件
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * 邮件
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * 照片
	 */
	public String getPhoto() {
		return photo;
	}

	/**
	 * 照片
	 */
	public void setPhoto(String photo) {
		this.photo = photo;
	}

	/**
	 * 住址所在省
	 */
	public String getSheng() {
		return sheng;
	}

	/**
	 * 住址所在省
	 */
	public void setSheng(String sheng) {
		this.sheng = sheng;
	}

	/**
	 * 住址所在市
	 */
	public String getShi() {
		return shi;
	}

	/**
	 * 住址所在市
	 */
	public void setShi(String shi) {
		this.shi = shi;
	}

	/**
	 * 住址所在县
	 */
	public String getXian() {
		return xian;
	}

	/**
	 * 住址所在县
	 */
	public void setXian(String xian) {
		this.xian = xian;
	}

	/**
	 * 住址具体地址
	 */
	public String getLocal() {
		return local;
	}

	/**
	 * 住址具体地址
	 */
	public void setLocal(String local) {
		this.local = local;
	}

	/**
	 * 学历
	 */
	public String getDegree() {
		return degree;
	}

	/**
	 * 学历
	 */
	public void setDegree(String degree) {
		this.degree = degree;
	}

	/**
	 * 民族
	 */
	public String getNation() {
		return nation;
	}

	/**
	 * 民族
	 */
	public void setNation(String nation) {
		this.nation = nation;
	}

	/**
	 * 入职时间
	 */
	public Date getEnterTime() {
		return enterTime;
	}

	/**
	 * 入职时间
	 */
	public void setEnterTime(Date enterTime) {
		this.enterTime = enterTime;
	}

	/**
	 * 毕业学校
	 */
	public String getGraduateSchool() {
		return graduateSchool;
	}

	/**
	 * 毕业学校
	 */
	public void setGraduateSchool(String graduateSchool) {
		this.graduateSchool = graduateSchool;
	}

	/**
	 * 其它属性json字符串
	 */
	public String getOtherAttributesJson() {
		return otherAttributesJson;
	}

	/**
	 * 其它属性json字符串
	 */
	public void setOtherAttributesJson(String otherAttributesJson) {
		this.otherAttributesJson = otherAttributesJson;
	}

	public JdbcUserAuthenticationEntity getAuthentication() {
		return authentication;
	}

	public void setAuthentication(JdbcUserAuthenticationEntity authentication) {
		this.authentication = authentication;
	}

	public Set<JdbcUserRoleEntity> getRoles() {
		return roles;
	}

	public void setRoles(Set<JdbcUserRoleEntity> roles) {
		this.roles = roles;
	}
	
	public String getSocialCreditCode() {
		return socialCreditCode;
	}

	public void setSocialCreditCode(String socialCreditCode) {
		this.socialCreditCode = socialCreditCode;
	}

	public Boolean getJobStatu() {
		return jobStatu;
	}

	public void setJobStatu(Boolean jobStatu) {
		this.jobStatu = jobStatu;
	}
	
	public Boolean getIsIdCheck() {
		return isIdCheck;
	}

	public void setIsIdCheck(Boolean isIdCheck) {
		this.isIdCheck = isIdCheck;
	}

	@Override
	public void setId(String arg0) {
		this.setUserid(arg0);
	}

	@Override
	public void from(UserInfoEntity user) {
		this.setUserid(user.getUserid());
		this.setUserName(user.getUserName());
		this.setBirth(user.getBirth());
		this.setDegree(user.getDegree());
		this.setDepartmentId(user.getDepartmentId());
		this.setEmail(user.getEmail());
		this.setEnterTime(user.getEnterTime());
		this.setGraduateSchool(user.getGraduateSchool());
		this.setLocal(user.getLocal());
		this.setNation(user.getNation());
		this.setOperatingPostId(user.getOperatingPostId());
		this.setOrganizationId(user.getOrganizationId());
		this.setOtherAttributesJson(user.getOtherAttributesJson());
		this.setPhoto(user.getPhoto());
		this.setPositionId(user.getPositionId());
		this.setSex(user.getSex());
		this.setSheng(user.getSheng());
		this.setShi(user.getShi());
		this.setXian(user.getXian());
		this.setTel(user.getTel());
		this.setWorkingGroupId(user.getWorkingGroupId());
		this.setJobStatu(user.getJobStatu());
		this.setIsIdCheck(user.getIsIdCheck());
		this.setSocialCreditCode(user.getSocialCreditCode());

		Set<JdbcUserRoleEntity> items = new HashSet<JdbcUserRoleEntity>();
		Set<UserRoleEntity> entityItems = user.getRoles();
		if (entityItems != null) {
			for (UserRoleEntity item : entityItems) {
				JdbcUserRoleEntity jEntity = new JdbcUserRoleEntity();
				jEntity.from(item);
				items.add(jEntity);
			}
		}
		this.setRoles(items);
	}

	@Override
	public UserInfoEntity toEntity() {
		UserInfoEntity entity = new UserInfoEntity();
		entity.setUserid(this.getUserid());
		entity.setUserName(this.getUserName());
		entity.setBirth(this.getBirth());
		entity.setDegree(this.getDegree());
		entity.setDepartmentId(this.getDepartmentId());
		entity.setEmail(this.getEmail());
		entity.setEnterTime(this.getEnterTime());
		entity.setGraduateSchool(this.getGraduateSchool());
		entity.setLocal(this.getLocal());
		entity.setNation(this.getNation());
		entity.setOperatingPostId(this.getOperatingPostId());
		entity.setOrganizationId(this.getOrganizationId());
		entity.setOtherAttributesJson(this.getOtherAttributesJson());
		entity.setPhoto(this.getPhoto());
		entity.setPositionId(this.getPositionId());
		entity.setSex(this.getSex());
		entity.setSheng(this.getSheng());
		entity.setShi(this.getShi());
		entity.setXian(this.getXian());
		entity.setTel(this.getTel());
		entity.setWorkingGroupId(this.getWorkingGroupId());
		entity.setJobStatu(this.getJobStatu());
		entity.setIsIdCheck(this.getIsIdCheck());
		entity.setSocialCreditCode(this.getSocialCreditCode());
		if (this.getAuthentication() != null) {
			entity.setAccounts(this.getAuthentication().getAccounts());
			entity.setValid(this.getAuthentication().getValid());
		} else {
			entity.setValid(false);
		}

		Set<UserRoleEntity> items = new HashSet<UserRoleEntity>();
		Set<JdbcUserRoleEntity> entityItems = this.getRoles();
		if (entityItems != null) {
			for (JdbcUserRoleEntity item : entityItems) {
				items.add(item.toEntity());
			}
		}
		entity.setRoles(items);

		return entity;
	}
}
